package com.fengba.nanzhi.setup;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by SpakeBrony on 2018/4/9.
 */

public class ForPhone02 extends AppCompatActivity {




    //新手机号
    private EditText FP02phone;


    //验证码
    private EditText FP02code;


    private Button FP02btn01;


    private TimeCount time;

    //短信编码
    private  String msg_id = "0";


    public ForPhone02() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_forphone02);
        FP02phone = (EditText) findViewById(R.id.FP02phone);
        FP02phone.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        FP02code = (EditText) findViewById(R.id.FP02code);
        FP02code.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        time = new TimeCount(60000, 1000);

        /**
         * 返回
         */
        ImageButton FP02btn02 = (ImageButton) findViewById(R.id.FP02btn02);
        FP02btn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        /**
         * 验证码
         */
        FP02btn01 = (Button) findViewById(R.id.FP02btn01);
        /*FP02btn01.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        FP02btn01.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                if (FP02phone.length() == 11) {
                    time.start();
                    OkHttpClient client = new OkHttpClient();
                    //构建FormBody，传入要提交的参数
                    FormBody formBody = new FormBody
                            .Builder()
                            .add("type", "0")
                            .add("mobile", String.valueOf(FP02phone.getText()))
                            .build();
                    final Request request = new Request.Builder()
                            .url(UrlUtil.UrlFX + "api/user/information")
                            .post(formBody)
                            .build();
                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            ToastUtil.showToast(ForPhone02.this,"网络错误",null);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            try {
                                String json = response.body().string();
                                JSONObject jsonObject = new JSONObject(json);
                                final String message = (String) jsonObject.get("message");
                                if (jsonObject.get("code").equals(0)) {
                                    JSONObject data = (JSONObject) jsonObject.get("data");
                                    msg_id = data.get("message").toString();
                                } else {
                                    ToastUtil.showToast(ForPhone02.this,message,null);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    });

                } else {
                    Toast.makeText(ForPhone02.this, "请输入正确手机号", Toast.LENGTH_SHORT).show();
                }
            }


        });*/

    }//都在这里打


    //计时器实现
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            FP02btn01.setClickable(false);
            FP02btn01.setText("("+millisUntilFinished / 1000 +") ");
        }

        @Override
        public void onFinish() {
            FP02btn01.setText("重新获取验证码");
            FP02btn01.setClickable(true);
        }
    }


    /**
     * 隐藏软键盘
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
    public  boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


}